﻿/* 
 *  <copyright file="PlayerLegAnimatorCmp.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using SobrietyEngine.Components;
using SobrietyEngine.Components.Graphic;
using SobrietyEngine.Components.Physics.OnCollision;
using SobrietyEngine.Logging;

namespace SEGameExample.Components
{
    class PlayerLegAnimatorCmp : BaseComponent, IComponent
    {
        AnimatedQuadCmp animCmp;
        IFarseerPhysicsBody physCmp;
        OnGroundCmp onGrndCmp;

        public PlayerLegAnimatorCmp()
            : base()
        {
        }

        public override Boolean InitComponent()
        {
            base.InitComponent();
            animCmp = Actor.Get<AnimatedQuadCmp>();
            physCmp = Actor.Get<IFarseerPhysicsBody>();
            onGrndCmp = Actor.Get<OnGroundCmp>();

            if (animCmp == null || physCmp == null || onGrndCmp == null)
            {
                Logger.Debug(this, "Missing animation and/or physics body cmps, delaying init");
                return false;
            }

            return true;
        }

        Vector2 velocity;
        float angVel;
        float vSq;
        float vSqrt;
        Vector2 textureDir = new Vector2(1, 0);
        public new void Update(GameTime elapsedTime)
        {            
            velocity = physCmp.Body.LinearVelocity;
            angVel = physCmp.Body.AngularVelocity;

            //if we are in the air
            if (onGrndCmp.IsOnGround == false && onGrndCmp.TimeOffGroundMs > 100)
            {
                animCmp.SetAnimation("Running");
                animCmp.AnimationSpeedMult = 0.0f;
                animCmp.SetFrame(13);
            }
            else
            {
                //if we are on the ground,                 
                vSq = velocity.LengthSquared();
                vSqrt = velocity.Length();

                //standing still
                if (vSq < 0.5f || Math.Abs(angVel) < 0.1f)
                {
                    animCmp.SetAnimation("Walking");
                    animCmp.AnimationSpeedMult = 0.0f;
                    animCmp.SetFrame(29);
                    return;
                }
                //walking
                else if (vSq < 8.75f)
                {
                    animCmp.SetAnimation("Walking");
                    animCmp.AnimationSpeedMult = vSqrt * 1.5f;
                }
                //running
                else
                {
                    animCmp.SetAnimation("Running");
                    animCmp.AnimationSpeedMult = vSqrt;
                }
            }

            //change our texture flip based on veocity
            float dot = Vector2.Dot(velocity, textureDir);
            if (dot < 0)
                animCmp.HFlip = true;
            else
                animCmp.HFlip = false;
        }       
    }
}
